﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Net.Sockets;
using System.Data.SqlClient;
using expUserInfo;

namespace expUserLoanInfo
{
    public partial class Form1 : Form
    {
        static string srvip = "192.168.1.80";
        public Form1()
        {
            InitializeComponent();
        }  

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            try
            {
                //获取连接字符串
                string connstr = a();
                //验证用户名和密码是否正确。
                b(null, null);
                //获取信息
                DataTable dt = new DataTable();
                ListItem selectedItem = this.cmbBankList.SelectedItem as ListItem;
                int bankid = int.Parse(selectedItem.Data.ToString());

                using (SqlConnection conn = new SqlConnection(connstr))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();

                    cmd.CommandText = "select U.aid as Uaid,cname,loansum,(case when loantype='true' then '等额本息' when loantype='false' then '等额本金' when loantype is null then '未填写' end) as loantype,convert(varchar(10),getloandate,120) as getloandate,mphone,livein,dealMan from CIMuserinfo as U,CIMbankinfo as B,CIMfirminfo as F where U.aid=B.aid and B.aid=F.aid and bankid=" + bankid.ToString();
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);
                    adp.Fill(dt);                   
                }
                //导出到本地库
                ExcelHelper excel = new ExcelHelper();
                excel.DataTableToExcel(dt, 1, 1);
                excel.OutputFilePath = "./userloan"+bankid.ToString()+".xls";
                excel.OutputExcelFile();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                button1.Enabled = true;
            }
        }

        private string a()
        {
            string hostname = srvip;
            int port = 6625;
            TcpClient client = new TcpClient();
            client.Connect(hostname, port);
            string connstr = string.Empty;
            using (NetworkStream stream = client.GetStream())
            {
                string s = "GetConnstr";
                byte[] bytes = new byte[0x400];
                bytes = Encoding.UTF8.GetBytes(s);
                stream.Write(bytes, 0, bytes.Length);
                byte[] buffer = new byte[0x400];
                int num2 = stream.Read(buffer, 0, buffer.Length);
                connstr = Encoding.UTF8.GetString(buffer).Replace("\0", "");
            }
            client.Close();
            return connstr;
        }

        private void b(object o, EventArgs e)
        {
            txtUser.Text = "xuluhss";
            if (string.IsNullOrEmpty(this.txtUser.Text))
            {
                throw new Exception("请输入用户名");       
            }
            else if (string.IsNullOrEmpty(this.txtPass.Text))
            {
                throw new Exception("请输入密码");              
            }
            else
            {
                string hostname =srvip;
                int port = 6625;
                TcpClient client = new TcpClient();
                client.Connect(hostname, port);
                using (NetworkStream stream = client.GetStream())
                {
                    string s = string.Concat(new object[] { "Check|", this.txtUser.Text.Trim(), "#", this.txtPass.Text.Trim(), "#1|1.0.0.0"});
                    byte[] bytes = new byte[0xff];
                    bytes = Encoding.UTF8.GetBytes(s);
                    stream.Write(bytes, 0, bytes.Length);
                    byte[] buffer = new byte[0x400];
                    int num2 = stream.Read(buffer, 0, buffer.Length);
                    string text = Encoding.UTF8.GetString(buffer).Replace("\0", "");
                    string[] strArray2 = text.Split(new char[] { '|' });
                    if (strArray2[0].Equals("Success", StringComparison.OrdinalIgnoreCase))
                    {                       
                        int PurviewID = int.Parse(strArray2[2].ToString());
                        if ((PurviewID & 15) == 0)
                        {//权限不够
                            MessageBox.Show("没有权限。");
                            return;
                        }
                    }
                    else
                    {
                        throw new Exception("登陆失败。");
                    }
                }
                client.Close();
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(a()))
            {
                connection.Open();
                SqlCommand command = connection.CreateCommand();
                command.CommandText = "select bankid,bankname From CIMbank";
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        ListItem item = new ListItem();
                        item.Data = reader["bankid"];
                        item.Text = reader["bankname"].ToString();
                        this.cmbBankList.Items.Add(item);
                    }
                }
            }
            if (this.cmbBankList.Items.Count > 0)
            {
                this.cmbBankList.SelectedIndex = 0;
            }

        }
    }
}
